package com.example.service;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.extension.api.R;
import com.example.common.Result;
import com.example.entity.*;
import com.example.exception.ServiceException;
import com.example.mapper.GreenHouseMapper;
import com.example.mapper.SensorMapper;
import com.example.mapper.UserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class UserService {

    @Resource
    UserMapper userMapper;
    @Resource
    GreenHouseMapper greenHouseMapper;
    @Resource
    SensorMapper sensorMapper;
    public boolean isAdmin(Integer uid) {
        User user = userMapper.getUserById(uid);
        if(user.getRole().getDesc().equals("用户")){
            return true;
        }else {
            return false;
        }
    }

    public Result register(User register) {
        if(register.getRole().getDesc().equals("管理员")){
            return Result.error("无法注册管理员权限用户");
        }
        User data = userMapper.getUserByUserName(register.getUsername());
        if(data != null){
            return Result.error("用户名已存在");
        }
        Date date = DateUtil.date();
        register.setCreatedAt(date);
        register.setUpdatedAt(date);
        userMapper.register(register);
        register.setCreatedBy(register.getId());
        register.setUpdatedBy(register.getId());
        userMapper.update(register);
        return Result.success("注册成功",register);
    }

    public Result addUser(User user) {
        User admin = userMapper.getUserById(user.getCreatedBy());
        if(!admin.getRole().getDesc().equals("管理员")){
            return Result.error("无法注册管理员权限用户");
        }
        User data = userMapper.getUserByUserName(user.getUsername());
        if(data != null){
            return Result.error("用户名已存在");
        }
        Date date = DateUtil.date();
        user.setCreatedAt(date);
        user.setUpdatedAt(date);
        userMapper.register(user);
        return Result.success("添加成功",user);
    }

    public Result deleteUser(User user) {
        User admin = userMapper.getUserById(user.getUpdatedBy());
        if(!admin.getRole().getDesc().equals("管理员")){
            return Result.error("权限不足");
        }
        Date date = DateUtil.date();
        user.setUpdatedAt(date);
        if(!userMapper.update(user)){
            return Result.error("删除失败");
        }
        return Result.success("删除成功",null);
    }

    public User login(User user) {
        User data = userMapper.getUserByUserName(user.getUsername());
        if(data!=null){
            if(data.getPassword().equals(user.getPassword())){
                Date date = DateUtil.date();
                data.setLastLoginTime(date);
                userMapper.update(data);
            }
        }
        return data;
    }

    public Result getUser(User user) {
        User data = userMapper.getUserById(user.getId());
        return Result.success(user);
    }

    public Result updateUser(User user) {
        if(!user.getId().equals(user.getUpdatedBy())){
            User admin = userMapper.getUserById(user.getUpdatedBy());
            if(!admin.getRole().getDesc().equals("管理员")){
                return Result.error("权限不足");
            }
        }
        Date date = DateUtil.date();
        user.setUpdatedAt(date);
        if(!userMapper.update(user)){
            return Result.error("更新失败");
        }
        return Result.success("更新成功");
    }

    public Result listUser(User user) {
        List<User> userList = new ArrayList<>();
        if(user.getId() != null){
            User userById = userMapper.getUserById(user.getId());
            userList.add(userById);
        } else if (user.getUsername() != null) {
            User userByUserName = userMapper.getUserByUserName(user.getUsername());
            userList.add(userByUserName);
        }else {
            userList = userMapper.getUserList(user);
        }
        return Result.success(userList);
    }

    public List<User> getUserList(User baseUsers) {
        return userMapper.getUserListByUserName(baseUsers.getUsername());
    }

    public Result equipment() {
        JSONObject json = new JSONObject();
        Integer greenHouseNum = greenHouseMapper.getNum();
        Integer sensorNum = sensorMapper.getNum();
        Integer sensorAliveNum = sensorMapper.getAliveNum();
        Integer sensorDeadNum = sensorNum - sensorAliveNum;
        List<JSONObject> typeNum = sensorMapper.getTypeNum();
        json.put("greenHouseNum",greenHouseNum);
        json.put("sensorNum",sensorNum);
        json.put("sensorAliveNum",sensorAliveNum);
        json.put("sensorDeadNum",sensorDeadNum);
        json.put("typeNum",typeNum);
        return Result.success(json);
    }
}
